package com.james;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @desc:
 * @auther: shipengfei
 * @date: 2022-7-3 14:09
 **/
@RestController
@RequestMapping("fillData")
@Slf4j
public class FillDataController {

    public List<Map<String, Object>> dataList = new ArrayList<>();

    /**
     * 执行导入
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public Map<String, Object> doImport(@RequestBody Map<String, Object> params) {
        dataList.add(params);

        sendData(params);

        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        return res;
    }

    private void sendData(Map<String, Object> params) {
        CopyOnWriteArraySet<MyWebSocket> webSocketSet = MyWebSocket.getWebSocketSet();
        log.info("推送数据：{}", params);
        //群发消息
        for (MyWebSocket item : webSocketSet) {
            try {
                item.sendMessage(params.get("data1") + "");
            } catch (Exception e) {
                log.error("数据推送失败", e);
            }
        }
    }
}
